package com.ruoyi.ysgl.yssb.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.ysgl.yssb.domain.YsglYssbFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.ysgl.yssb.domain.YsglYssb;
import com.ruoyi.ysgl.yssb.service.IYsglYssbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预算申报Controller
 *
 * @author huacai
 * @date 2024-08-08
 */
@RestController
@RequestMapping("/yssb/yssb")
public class YsglYssbController extends BaseController
{
    @Resource
    private IYsglYssbService ysglYssbService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询预算申报列表
     */
    @PreAuthorize("@ss.hasPermi('yssb:yssb:list')")
    @GetMapping("/list")
    public TableDataInfo list(YsglYssb ysglYssb)
    {
        startPage();
        List<YsglYssb> list = ysglYssbService.selectYsglYssbList(ysglYssb);
        return getDataTable(list);
    }

    /**
     * 导出预算申报列表
     */
    @PreAuthorize("@ss.hasPermi('yssb:yssb:export')")
    @Log(title = "预算申报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YsglYssb ysglYssb)
    {
        List<YsglYssb> list = ysglYssbService.selectYsglYssbList(ysglYssb);
        ExcelUtil<YsglYssb> util = new ExcelUtil<YsglYssb>(YsglYssb.class);
        util.exportExcel(response, list, "预算申报数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<YsglYssb> util = new ExcelUtil<YsglYssb>(YsglYssb.class);
        util.importTemplateExcel(response, "预算申报数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "预算申报", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('yssb:yssb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<YsglYssb> util = new ExcelUtil<YsglYssb>(YsglYssb.class);
        InputStream inputStream = file.getInputStream();
        List<YsglYssb> list = util.importExcel(inputStream );
        inputStream.close();
        int count = ysglYssbService.batchInsertYsglYssb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取预算申报详细信息
     */
    @PreAuthorize("@ss.hasPermi('yssb:yssb:query')")
    @GetMapping(value = "/{yssbId}")
    public AjaxResult getInfo(@PathVariable("yssbId") String yssbId)
    {
        return success(ysglYssbService.selectYsglYssbByYssbId(yssbId));
    }

    /**
     * 新增预算申报
     */
    @PreAuthorize("@ss.hasPermi('yssb:yssb:add')")
    @Log(title = "预算申报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YsglYssb ysglYssb) {
        ysglYssb.setUserId(Math.toIntExact(getUserId()));
        int i = ysglYssbService.insertYsglYssb(ysglYssb);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(ysglYssb.getYssbId());
        infoRunTaskData.setBusinessType(9); //业务模块: 预算管理-预算申报
        infoRunTaskData.setBusinessTitle(ysglYssb.getSsDw() + ysglYssb.getNd()); //任务名称: 所属单位 + 年度
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改预算申报
     */
    @PreAuthorize("@ss.hasPermi('yssb:yssb:edit')")
    @Log(title = "预算申报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YsglYssb ysglYssb) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(ysglYssb.getSsDw() + ysglYssb.getNd()); //任务名称: 所属单位 + 年度
        infoRunTaskData.setBusinessKey(ysglYssb.getYssbId());
        infoRunTaskData.setBusinessType(9); //业务模块: 预算管理-预算申报
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(ysglYssbService.updateYsglYssb(ysglYssb));
    }

    /**
     * 删除预算申报
     */
    @PreAuthorize("@ss.hasPermi('yssb:yssb:remove')")
    @Log(title = "预算申报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{yssbIds}")
    public AjaxResult remove(@PathVariable String[] yssbIds) {
        //同时删除业务关系数据
        for (String yssbId : yssbIds) {
            YsglYssb ysglYssb = ysglYssbService.selectYsglYssbByYssbId(yssbId);
            iInfoRunTaskDataService.deleteData(ysglYssb.getYssbId(), 9);//业务模块: 预算管理-预算申报
        }
        return toAjax(ysglYssbService.deleteYsglYssbByYssbIds(yssbIds));
    }

    /**
     * 获得预算申报主键ID (UUID)
     */
    @GetMapping("/getYsSbId")
    public AjaxResult getYsSbId(){
        String ysSb = IdUtils.fastSimpleUUID();
        return success(ysSb);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertYssbFjByYssbId(@RequestBody YsglYssbFj ysglYssbFj){
        return toAjax(ysglYssbService.insertYssbFjByYssbId(ysglYssbFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectYssbFjList(String sId){
        YsglYssbFj ysglYssbFj = new YsglYssbFj();
        ysglYssbFj.setYssbId(sId);
        return AjaxResult.success(ysglYssbService.selectYssbFjList(ysglYssbFj));
    }

    /**
     * 删除预算申报附件
     */
    @Log(title = "预算申报附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(ysglYssbService.deleteYsglYssbFjByFjIds(fjIds));
    }

}
